package free.mrchen.boot.gate.server.endpoint;

import free.mrchen.boot.gate.server.def.RouteDefFeature;
import free.mrchen.boot.gate.server.vo.RVo;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestControllerEndpoint( id = "routeStable" )
public class RouteStableControllerEndpoint {

    private final RouteDefFeature routeDefFeature;

    public RouteStableControllerEndpoint(RouteDefFeature routeDefFeature) {
        this.routeDefFeature = routeDefFeature;
    }

    @GetMapping("/definitions")
    public Mono<RVo> routeDefinitions() {
        return Flux.fromIterable( routeDefFeature.listStable().values() )
                .collectList()
                .map( RVo::success )
                .map(RVo.Builder::build);
    }


    @GetMapping("/definition/{id}")
    public  Mono<RVo> getRouteDefinition(
            @PathVariable("id") String id
    ) {
        return Flux.fromIterable( routeDefFeature.listStable().values() )
                .filter( it -> id.equals( it.getId() ))
                .singleOrEmpty()
                .map( RVo::success )
                .map(RVo.Builder::build);
    }






}
